<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class manajemen extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('m_manajemen');
    }

    function index() {
        $data['list'] = $this->m_manajemen->get_all_manajemen();
        $data['konten'] = 'v_manajemen';
        $this->load->view('theme', $data);
    }

    function tambah_manajemen() {
        $data['list'] = $this->m_manajemen->get_all_jabatan();
        $data['konten'] = 'v_manajemen_tambah';
        $this->load->view('theme', $data);
    }

    function fungsi_tambah_manajemen() {
        $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('id_jabatan', 'Jabatan', 'trim|required');

        $data['nama'] = $this->input->post('nama');
        $data['username'] = $this->input->post('username');
        $data['password'] = $this->input->post('password');
        $data['email'] = $this->input->post('email');
        $data['id_jabatan'] = $this->input->post('id_jabatan');

        if ($this->form_validation->run() == TRUE) {
            $res = $this->m_manajemen->insert_manajemen($data);
            header('location:' . site_url() . "manajemen/" . $this->index());
        } else {
            $data['list'] = $this->m_manajemen->get_all_manajemen();
            $data['konten'] = 'v_manajemen_tambah';
            $this->load->view('theme', $data);
        }
    }

    function edit_manajemen() {
        $data['list'] = $this->m_manajemen->get_all_jabatan();
        $id = $this->uri->segment(3);
        $data['manajemen'] = $this->m_manajemen->get_by_id($id);
        $data['konten'] = 'v_manajemen_edit';
        $this->load->view('theme', $data);
    }

    function update_manajemen() {
        $data['nama'] = $_POST['nama'];
        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        $data['email'] = $_POST['email'];
        $data['id_jabatan'] = $_POST['id_jabatan'];
        $res = $this->m_manajemen->update_manajemen($data, $_POST['id_manajemen']);
        if ($res) {
            header('location:' . base_url() . "manajemen/" . $this->index());
        }
    }

    function hapus_manajemen($id_manajemen) {
        $this->m_manajemen->delete_manajemen($id_manajemen);
        $this->index();
    }

    function lihat_jabatan() {
        $data['list'] = $this->m_manajemen->get_all_jabatan();
        $data['konten'] = 'v_jabatan';
        $this->load->view('theme', $data);
    }

    function tambah_jabatan() {
        $data['konten'] = 'v_jabatan_tambah';
        $this->load->view('theme', $data);
    }

    function fungsi_tambah_jabatan() {
        $this->form_validation->set_rules('nama_jabatan', 'Nama Jabatan', 'trim|required');
        $this->form_validation->set_rules('keterangan', 'Keterangan', 'trim|required');

        $data['nama_jabatan'] = $this->input->post('nama_jabatan');
        $data['keterangan'] = $this->input->post('keterangan');
        //$res = $this->m_manajemen->insert_jabatan($data);
        // if($res){
        // 	header('location:'.base_url()."manajemen/lihat_jabatan".$this->index());
        // }
        if ($this->form_validation->run() == TRUE) {
            $res = $this->m_manajemen->insert_jabatan($data);
            header('location:' . site_url() . "manajemen/lihat_jabatan" . $this->index());
        } else {
            $data['list'] = $this->m_manajemen->get_all_jabatan();
            $data['konten'] = 'v_jabatan_tambah';
            $this->load->view('theme', $data);
        }
    }

    function edit_jabatan() {
        $id = $this->uri->segment(3);
        $data['jabatan'] = $this->m_manajemen->get_jabatan_by_id($id);
        $data['konten'] = 'v_jabatan_edit';
        $this->load->view('theme', $data);
    }

    function update_jabatan() {
        $data['nama_jabatan'] = $_POST['nama_jabatan'];
        $data['keterangan'] = $_POST['keterangan'];
        $res = $this->m_manajemen->update_jabatan($data, $_POST['id_jabatan']);
        if ($res) {
            header('location:' . base_url() . "manajemen/lihat_jabatan" . $this->index());
        }
    }

    function hapus_jabatan($id_jabatan) {
        $this->index();
        $cek_jabatan = $this->m_manajemen->cek_jabatan($id_jabatan);
        if ($cek_jabatan == TRUE) {
            if ($this->form_validation->run() == FALSE) {
                $res = $this->m_manajemen->delete_jabatan($id_jabatan);
                header('location:' . site_url() . "manajemen/lihat_jabatan" . $this->index());
            } else {
                $this->index();
            }
        } else {
            $this->session->set_flashdata('result_pass', 'Jabatan masih digunakan!');
            header('location:' . site_url('manajemen/lihat_jabatan'));
        }
    }

    function hak_akses_manajemen() {
        $data['list'] = $this->m_manajemen->get_all_jabatan();
        $data['menu'] = $this->m_manajemen->get_all_menu()->result();
        
        $data['konten'] = 'v_hak_akses_manajemen';
        $this->load->view("theme", $data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */